using global::GoGameClient.script.global;
using Godot;
using GoGameClient.script.cmd;
using GoGameClient.script.network.webSocket;

namespace GoGameClient.script.node.letter;

public partial class LetterByCharacterGui : Control 
{
    // Called when the node enters the scene tree for the first time.
    private OffsetControl _offsetControl;
    private AnimatedSprite2D _character;
    private Sprite2D _timeSprite;

    public override void _Ready()
    {
        _timeSprite = GetNode<Sprite2D>("TimeSprite");
        _offsetControl = GetNode<OffsetControl>("OffsetControl");
        _character = GetNode<AnimatedSprite2D>("Character");
        SocketControl.Instance().OfListen(new ListenBroadcastConfig<TimeBroadcastMessage>
        {
            Title = "时间广播监听",
            Cmd = (int)Router.Broadcast,
            CmdMethod = (int)BroadcastRouter.Time,
            Result = TimeUpdate
        });
        SocketControl.Instance().Request(new RequestConfig<TimeBroadcastMessage>
        {
            Title = "当前时间",
            Cmd = (int)Router.Common,
            CmdMethod = (int)CommonRouter.CurrentTime,
            Result = TimeUpdate
        });
    }

    private void TimeUpdate(TimeBroadcastMessage message)
    {
        _timeSprite.Texture = GD.Load<Texture2D>($"res://resources/Ui/time-vertical/{message.Time}.png");
        SetNight(message.IsNight);
    }

    public void SetMapName(string name)
    {
        GetNode<Label>("MapName").Text = name;
    }

    public void SetPosition(Vector2I vector)
    {
        GetNode<Label>("Position").Text = $"Y:{vector.Y} X:{vector.X}";
    }

    public void SetNight(bool night)
    {
        _offsetControl.SetNight(night);
    }


    public void SetMove(bool move)
    {
        _offsetControl.SetMove(move);
        _character.Play(move ? "run" : "stop");
    }
}